<template>
  <div class="app-container">
    <el-form :model="searchForm" ref="searchFormRef" size="small" inline v-show="showSearch">
      <el-form-item label="字典名称" prop="like.name">
        <el-input v-model="searchForm.like.name" placeholder="请输入字典名称" clearable @keyup.enter="search"/>
      </el-form-item>
      <el-form-item label="字典类型" prop="like.type">
        <el-input v-model="searchForm.like.type" placeholder="请输入字典类型" clearable @keyup.enter="search"/>
      </el-form-item>
      <el-form-item label="创建时间" prop="time.create_time">
        <el-date-picker
          v-model="searchForm.time.create_time"
          value-format="yyyy-MM-dd"
          style="width: 220px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb-8px reset">
      <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd" v-hasPerm="['system:dict-type:add']">新增</el-button>
      <el-button type="success" plain icon="el-icon-edit" @click="handleEdit" v-hasPerm="['system:dict-type:edit']" :disabled="ids.length !== 1">修改</el-button>
      <el-button type="danger" plain icon="el-icon-delete" @click="handleDelete" v-hasPerm="['system:dict-type:delete']" :disabled="!ids.length">删除</el-button>
      <el-button type="warning" plain icon="el-icon-download" @click="handleExport" v-hasPerm="['system:dict-type:export']">导出</el-button>
      <el-button type="danger" plain icon="el-icon-refresh" @click="handleCache" v-hasPerm="['system:dict-type:cache']">刷新缓存</el-button>
      <right-toolbar :showSearch.sync="showSearch" @refresh="getList"/>
    </div>

    <el-table v-loading="loading" :data="listData" @selection-change="handleSelection">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="编号" width="60" align="center" prop="id"/>
      <el-table-column label="字典名称" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column label="字典类型" align="center" show-overflow-tooltip>
        <template v-slot="{ row }">
          <router-link :to="{ name: 'DictData', params: { dictId: row.id } }" class="link-type">{{ row.type }}</router-link>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" width="150">
        <template v-slot="{ row }">{{ parseTime(row.create_time) }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="120px">
        <template v-slot="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)" v-hasPerm="['system:dict-type:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" v-hasPerm="['system:dict-type:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-if="total > 0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="getList"/>
    <dict-form v-model="formShow" :module-name="moduleName" :row="formData" @submit="getList" v-if="formShow"/>
  </div>
</template>

<script>
import { listTypeApi, delTypeApi, refreshCacheApi, exportTypeApi } from '@/api/system/dict/type'
import DictForm from './form.vue'

export default {
  name: 'Dict',
  components: { DictForm },
  data() {
    return {
      moduleName: '字典类型',
      loading: true,
      ids: [],
      showSearch: true,
      total: 0,
      listData: [],
      formData: null,
      formShow: false,
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        like: { name: undefined, type: undefined },
        where: { status: undefined },
        time: { create_time: [] }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listTypeApi(this.searchForm).then(res => {
        this.listData = res.list
        this.total = res.total
        this.loading = false
      })
    },
    search() {
      this.searchForm.pageNum = 1
      this.getList()
    },
    reset() {
      this.resetForm('searchFormRef')
      this.search()
    },
    handleAdd() {
      this.formData = null
      this.formShow = true
    },
    handleEdit(row) {
      const editId = row?.id || (this.ids.length ? this.ids[0] : "")
      if (!editId) return this.$modal.msgWarning('参数缺失')
      this.formData = { id: editId }
      this.formShow = true
    },
    handleSelection(selection) {
      this.ids = selection.map(item => item.id)
    },
    handleDelete(row) {
      const dictIds = row?.id || this.ids.join(',')
      if (!dictIds) return this.$modal.msgWarning('未选择数据')
      this.$modal.confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？').then(() => {
        return delTypeApi(dictIds)
      }).then((res) => {
        this.getList()
        this.$modal.msgSuccess(res.msg || '删除成功')
      }).catch(() => {})
    },
    handleCache() {
      refreshCacheApi().then(() => {
        this.$modal.msgSuccess('刷新成功')
        this.$store.dispatch('dict/cleanDict')
      })
    },
    handleExport() {
      exportTypeApi(this.searchForm)
    }
  }
}
</script>
